<?php
class CCMIS_Controller extends MX_Controller {
    public function __construct() {
        parent::__construct();

        if(!isset($_SESSION['username']))
        {
            $this->session->set_userdata('username','');
        }
        if(!isset($_SESSION['logged_in']))
        {
            $this->session->set_userdata('logged_in',false);
        }
        if(!isset($_SESSION['sess_id']))
        {
            $this->session->set_userdata('sess_id',session_id());
        }
        if(!isset($_SESSION['user_id']))
        {
            $this->session->set_userdata('user_id',-1);
        }
        if(!isset($_SESSION['usertype']))
        {
            $this->session->set_userdata('usertype',0);
        }

        $newdata = array(
            'base_url'  => base_url(),
        );
        $this->session->set_userdata($newdata);

        date_default_timezone_set('Asia/Manila');


        $this->load->model('model_facultymember');
        $this->load->model('model_studentmember');
    }

    protected function setup_view($view_name, $data = array())
    {
        //$CI =& get_instance();
        $data = array_merge($this->session->all_userdata(), $data);
        $this->load->view('templates/header', $data);
        $this->load->view('templates/hidden', $data);
        $this->loadSideNavBar($view_name, $data);
        $this->load->view($view_name, $data);
        $this->load->view('templates/footer', $data);
    }

    private function loadSideNavBar($view_name, $data)
    {
        if(count(explode('/',$view_name)) <= 2)
        {
            $this->load->view('templates/sidebarout', $data);
        }
        else
        {
            $moduleName = $this->getModuleName($view_name);
            $this->load->view($moduleName.'/templates/sidebar', $data);
        }
    }

    private function getModuleName($view_name)
    {
        //get module name base on format >>> "[module]/[controller]/[action]
        $moduleName = explode('/',$view_name)[0];
        return $moduleName; //sample lang... fmis module muna lahat
    }

    public function login()
    {
        if($this->input->server('REQUEST_METHOD') === 'GET')
        {
            $data = array(
                'page_title' => 'Log in',
                'username' => '',
                'password' => '',
                'message' => '&nbsp;',
                'acctype' => 'student',
            );
            $this->setup_view('login', $data);
            return;
        }
        else //post request
        {
            $username = $this->input->post('username');//$_POST['username'];
            $password = $this->input->post('password');//$_POST['password'];
            $acctype = $this->input->post('accounttype');
            $message = $this->input->post('message');//$_POST['password'];

            if($acctype == "faculty")
            {
                $retVal = $this->model_facultymember->check_account($username, $password);
                if($retVal['status'])
                {
                    $newdata = array(
                        'logged_in'  => true,
                        'username' => $retVal['username'],
                        'user_id' => $retVal['user_id'],
                        'usertype' => $acctype,
                    );
                    $this->session->set_userdata($newdata);

                    $this->setup_view('home');
                }
                else //password and username incorrect
                {
                    $data = array(
                        'page_title' => 'Log in',
                        'username' => $username,
                        'password' => '',
                        'message' => $retVal['message'],
                        'acctype' => $acctype,
                    );
                    $this->setup_view('login', $data);
                }
            }
            else  if($acctype == "student")
            {
                $retVal = $this->model_studentmember->check_account($username, $password);
                if($retVal['status'])
                {
                    $newdata = array(
                        'logged_in'  => true,
                        'username' => $retVal['username'],
                        'user_id' => $retVal['user_id'],
                        'usertype' => $acctype,
                    );
                    $this->session->set_userdata($newdata);

                    $this->setup_view('home');
                }
                else //password and username incorrect
                {
                    $data = array(
                        'page_title' => 'Log in',
                        'username' => $username,
                        'password' => '',
                        'message' => $retVal['message'],
                        'acctype' => $acctype,
                    );
                    $this->setup_view('login', $data);
                }
            }
        }
    }
    public function logout()
    {
        session_destroy();
    	redirect('home');
    }

    //private

    public $mime_desc = array(
        'documents' => array(
            'application/powerpoint' => "PowerPoint Presentation",
            'application/vnd.ms-powerpoint' => "PowerPoint Presentation",
            'application/vnd.openxmlformats-officedocument.presentationml.presentation' => "PowerPoint Presentation",
            'application/excel' => "Excel Spreadsheet",
            'application/vnd.ms-excel' => "Excel Spreadsheet",
            'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' => "Excel Spreadsheet",
            'application/msexcel' => "Excel Spreadsheet",
            'application/pdf' => "Portable Document Format",
            'application/msword' => "Word Document",
            'application/vnd.openxmlformats-officedocument.wordprocessingml.document' => "Word Document",
            'text/html' => "HyperText Markup Language File",
            'text/latex' => "TeX File",
            'text/rtf' => "Rich Text File",
            'text/plain' => "Text File"
        ),
        'images' => array(
            'jpeg' => "JPEG Image"
            // TODO add more in these...
        )
    );
}